﻿Imports System
Imports System.Globalization
Imports System.IO
Imports System.Runtime.InteropServices

Namespace System.Workflow.ComponentModel.Compiler
    Friend Class Culture
        ' Methods
        Friend Shared Function GetItemCultureInfo(ByVal name As String) As ItemCultureInfo
            Dim info As ItemCultureInfo
            info.culture = Nothing
            Dim fileNameWithoutExtension As String = Path.GetFileNameWithoutExtension(name)
            Dim extension As String = Path.GetExtension(fileNameWithoutExtension)
            Dim flag As Boolean = False
            If ((Not extension Is Nothing) AndAlso (extension.Length > 1)) Then
                extension = extension.Substring(1)
                flag = Culture.IsValidCultureString(extension)
            End If
            If flag Then
                If ((info.culture Is Nothing) OrElse (info.culture.Length = 0)) Then
                    info.culture = extension
                End If
                Dim str3 As String = Path.GetExtension(name)
                Dim str4 As String = Path.GetFileNameWithoutExtension(fileNameWithoutExtension)
                Dim directoryName As String = Path.GetDirectoryName(name)
                Dim str6 As String = (str4 & str3)
                info.cultureNeutralFilename = Path.Combine(directoryName, str6)
                Return info
            End If
            info.cultureNeutralFilename = name
            Return info
        End Function

        Private Shared Function IsValidCultureString(ByVal cultureString As String) As Boolean
            If (Culture.cultureInfoStrings Is Nothing) Then
                Dim cultures As CultureInfo() = CultureInfo.GetCultures(CultureTypes.AllCultures)
                Culture.cultureInfoStrings = New String(cultures.Length  - 1) {}
                Dim i As Integer
                For i = 0 To cultures.Length - 1
                    Culture.cultureInfoStrings(i) = cultures(i).ToString.ToLowerInvariant
                Next i
                Array.Sort([Of] String)(Culture.cultureInfoStrings)
            End If
            Dim flag As Boolean = True
            If (Array.BinarySearch([Of] String)(Culture.cultureInfoStrings, cultureString.ToLowerInvariant) < 0) Then
                flag = False
            End If
            Return flag
        End Function


        ' Fields
        Private Shared cultureInfoStrings As String()

        ' Nested Types
        <StructLayout(LayoutKind.Sequential)> _
        Friend Structure ItemCultureInfo
            Friend culture As String
            Friend cultureNeutralFilename As String
        End Structure
    End Class
End Namespace

